/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __RGCPP_TCPSOCKETPOOL_H__
#define __RGCPP_TCPSOCKETPOOL_H__

#include <string>
#include <errno.h>
#include <string.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <boost/thread/mutex.hpp>

#include "rgcpp/structs/Map.h"
#include "rgcpp/structs/Types.h"

namespace rgcpp
{

class TCPSocket;
class TCPSocketPool
{
	protected:
		int maxFD;
		bool running;
		fd_set fdList;
		int pipeFD[2];
		boost::mutex mut;
		char pipeBuffer[1];
		boost::mutex mutRead;
		struct timeval timeout;
		Map<int,Types<TCPSocket*,bool> > socketFDList;

	public:
		TCPSocketPool();
		~TCPSocketPool();

		void addSocket(TCPSocket* socket);
		TCPSocket* getActiveSocket();
		void releaseSocket(TCPSocket* socket);
		void removeSocket(TCPSocket* socket);

		void setTimeout(const unsigned long timeout);
		unsigned long getTimeout();

		void release();
};

}

#endif
